//
//  LRXHelpMesgData.m
//  路人侠
//
//  Created by zhangxulong on 5/22/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXHelpMesgData.h"

@interface LRXHelpMesgData(){
    std::list<LRXHelpMeInfoModel *>items;
}
/**
 *  page 当前的页数
 */
@property (nonatomic,assign) NSInteger  page;
/**
 *  created_at 系统时间
 */
@property (nonatomic,copy) NSString * created_at;

@property (nonatomic,assign) NSInteger firstFlag;

@property (nonatomic,copy) TableViewLoadDataBlock  loadDataBlock;
@property (nonatomic,copy) TableViewLoadFaildBlock  loadDataFaildBlock;
@property (nonatomic,copy) TableViewLoadDataBlock  MoreDataBlock;
@property (nonatomic,copy) TableViewLoadFaildBlock  MoreDataFailBlock;

@end


@implementation LRXHelpMesgData

- (instancetype)initWithItemsWithLoadDatablock:(TableViewLoadDataBlock) loadDataBlock
                            loadDataFaildBlock:(TableViewLoadFaildBlock) loadDataFaildBlock
                             WithMoreDataBlock:(TableViewLoadDataBlock) MoreDataBlock
                            moreDataFaildBlock:(TableViewLoadFaildBlock) moreDataFaildBlock;
{
    if (self = [super init]) {
        self.loadDataBlock = [loadDataBlock copy];
        self.loadDataFaildBlock = [loadDataFaildBlock copy];
        self.MoreDataBlock = [MoreDataBlock copy];
        self.MoreDataFailBlock = [moreDataFaildBlock copy];
        
        self.page = 1;
        self.created_at = @"0000000000";
        self.firstFlag = 0;
    }
    return self;
}

//刷新数据
- (void)loadNewInfo
{
    
    //如果flag的值是0 第一页的数据 打开app第一次请求
    //如果flag的值是1 发布一次请求后 以后的数据
    
    
    NSDictionary * parm;
    //第一页的数据
    if (self.firstFlag == 0)
        parm = @{@"page":@"1"};
    else//刷新的最新数据
        parm = @{@"newest_data_time":self.created_at};
    
    __weak id weakSelf = self;
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    manger.requestSerializer.timeoutInterval = 10;
    [manger GET:mainUrl parameters:parm progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        LRXHelpMesgData * strongSelf = weakSelf;
         dispatch_async(dispatch_get_main_queue(), ^{
            NSArray * nextPage = responseObject[@"data"][@"dataset"];
            
            if (nextPage != nil && nextPage.count > 0){
                strongSelf.created_at = [LRXTimeStamp timeToTimeStamp:responseObject[@"data"][@"dataset"][0][@"created_at"]];
                //内容
                for (NSDictionary * dic in responseObject[@"data"][@"dataset"]) {
                    
                    LRXHelpMeInfoModel * p = [LRXHelpMeInfoModel helpModelWithDic:dic];
                    //第一次
                    if (strongSelf.firstFlag == 0)
                        items.push_back(p);
                    else
                        items.push_front(p);
                }
                //已加载第一页的数据了
                strongSelf.firstFlag = 1;
            }
    
             strongSelf.loadDataBlock(items,nextPage);
         });
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        LRXHelpMesgData * strongSelf = weakSelf;
        [MBProgressHUD showError:@"网络出现错误"];
        strongSelf.loadDataFaildBlock();
        
    }];
    
}
- (void)loadMoreInfo
{
    [self loadMoreInfo:mainUrl];
}
//加载更多的数据
- (void)loadMoreInfo:(NSString *)url
{
    __weak id weakSelf = self;
    //有下一页
    if (self.page > 0)
    {
        NSDictionary * parm = @{@"page":[NSString stringWithFormat:@"%ld",self.page + 1] };
        AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
        [manger GET:url parameters:parm progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            LRXHelpMesgData * strongSelf = weakSelf;
            dispatch_async(dispatch_get_main_queue(), ^{
                NSString * nextPage = responseObject[@"data"][@"next_page"];
                if (nextPage != nil && nextPage.length > 0) {
                    strongSelf.page++;
                    for (NSDictionary * dic in responseObject[@"data"][@"dataset"]) {
                        LRXHelpMeInfoModel * helpMesgs = [LRXHelpMeInfoModel helpModelWithDic:dic];
                        items.push_back(helpMesgs); //尾部添加新的数据
                    }
           
                }
                strongSelf.MoreDataBlock(items,nextPage);
            });
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            LRXHelpMesgData * strongSelf = weakSelf;
            strongSelf.MoreDataFailBlock();
        }];
    }
}
@end
